<?php
class Admin_OrganController extends Protox_Controller_Action
{

	public function indexAction()
	{
		$this->_helper->redirector('list', 'organ','admin');
	}

	public function listAction()
	{
		$this->view->pageTitle = 'Gerenciar orgãos';

		$organsDbTable = new Admin_Model_Organs();

		$form = new Admin_Form_OrganSearch();
		$request = $this->getRequest();

		$this->view->form = $form;

		if ($request->isPost()) {
			if ($form->isValid($request->getPost())) {
				$name = strtolower($form->getValue('name'));
				$acronym = strtolower($form->getValue('acronym'));

				$select = $organsDbTable->findOrgans($name,$acronym);

				if(sizeOf($select)==0) {
					$this->view->priorityMessenger(sprintf('Nenhum orgão encontrado'), 'alert');
				}
			}
		} else {
			$select = $organsDbTable->getOrgans();
		}

		$paginator = Zend_Paginator::factory($select);
		$page = $this->_getParam('page', 1);
		$paginator->setCurrentPageNumber($page);
		$paginator->setItemCountPerPage(20);
		
		$this->view->total = count($select);
		$this->view->paginator = $paginator;
	}

	public function addAction()
	{
		$this->view->pageTitle = 'Inserir novo orgão';

		$form = new Admin_Form_OrganAdd();
		$request = $this->getRequest();

		$this->view->form = $form;

		try {
			if ($request->isPost()) {
				if ($form->isValid($request->getPost())) {
					$name = $form->getValue('name');
					$acronym = $form->getValue('acronym');
					$date_created = date('Y-m-d');

					$organsDbTable = new Admin_Model_Organs();

					if(!$organsDbTable->isUniqueOrganName($name)) {
						$this->view->priorityMessenger(sprintf('Já exite um orgão cadastrado com o nome "%s"', $name), 'error');
						return false;
					}

					if(!$organsDbTable->isUniqueOrganAcronym($acronym)) {
						$this->view->priorityMessenger(sprintf('Já exite um orgão cadastrado a sigla "%s"', $acronym), 'error');
						return false;
					}

					$organsDbTable->addOrgan($name, $acronym, $date_created);

					$this->view->priorityMessenger(sprintf('Orgão cadastrado "%s (%s)"', $name, $acronym), 'sucess');

					$this->_helper->redirector('list', 'organ','admin');
				} else {
					$form->populate($this->getRequest()->getPost());
				}
			}

		} catch(Exception $ex) {
			echo $ex->getMessage();
			return false;
		}
	}

	public function updateAction()
	{
		$this->view->pageTitle = 'Editar orgão';

		$form = new Admin_Form_OrganAdd();
		$request = $this->getRequest();

		try {
			
			$id = $this->_getParam('id',0);

			if (!is_null($id)) {
				$organsDbTable = new Admin_Model_Organs();
				$select = $organsDbTable->getOrganById($id);
					
				if(false===$select) {
					$this->view->priorityMessenger(sprintf('Nenhum orgão localizado com id "%s"', $id), 'error');
					return false;

				} else {
					$this->view->form = $form;
					$form->populate($select);
					$this->view->data = $select;

					if ($request->isPost()) {
						if ($form->isValid($request->getPost())) {
							$name = $form->getValue('name');
							$acronym = $form->getValue('acronym');

							$organsDbTable->updateOrgan($id,$name,$acronym);

							$this->view->priorityMessenger(sprintf('Orgão "%s (%s)" editado com sucesso', $select['name'],$select['acronym']), 'sucess');

							$this->_helper->redirector('list', 'organ','admin');

						} else {
							$form->populate($this->getRequest()->getPost());
						}
					}
				}

			}
		} catch(Exception $ex) {
			return false;
		}
	}

	public function deleteAction()
	{
		$this->view->pageTitle = 'Excluir orgão';

		$form = new Admin_Form_DeleteConfirm();
		$request = $this->getRequest();

		$id = $this->_getParam('id',0);
		if (!is_null($id)) {
			$organsDbTable = new Admin_Model_Organs();
			$select = $organsDbTable->getOrganById($id);
				
			if(false===$select) {
				$this->view->priorityMessenger(sprintf('Nenhum orgão localizado com id "%s"', $id), 'error');
				return false;

			} else {
				$this->view->data = $select;

				if ($request->isPost()) {
					if ($form->isValid($request->getPost())) {

						$respYes = $form->getValue('yes');
						$respNo = $form->getValue('no');

						if(!is_null($respYes)) {
							$this->view->priorityMessenger(sprintf('Orgão excluido "%s (%s)"', $select['name'], $select['acronym']), 'sucess');
							$organsDbTable->deleteOrgan($id);
							$this->_helper->redirector('list', 'organ','admin');
						}

						if(!is_null($respNo)) {
							$this->_helper->redirector('list', 'organ','admin');
						}

					}
				}

				$this->view->form = $form;
			}
		}
	}
	

}